const express = require('express');
const app = express();
const port = 3000;


// 定义要返回的内容数组  
const contentArray = [{ "name": "首页", "id": "001" }, { "name": "手机", "id": "003" }, { "name": "电脑", "id": "002" }, { "name": "食品", "id": "004" },
{ "name": "男装", "id": "005" }, { "name": "生鲜", "id": "006" }, { "name": "家具厨具", "id": "007" }];



// 定义要返回的内容数组  
const bannerArray = ["https://img-blog.csdnimg.cn/direct/c524737d766545eebe2c96c0170bc891.png",
"https://img-blog.csdnimg.cn/direct/0a85d7196ac24a95adca22147743dfce.png",
"https://img-blog.csdnimg.cn/direct/8619bd87663c4ef4814612a9eafbfb60.png"];
// 创建API接口  
app.get('/api/gettabs', (req, res) => {
    // 定义响应对象  
    const response = {
        code: 200, // 通常200表示成功  
        data: {
            datas: contentArray
        },
        msg: "获取成功"
    };

    // 将响应对象转换为JSON并发送给客户端  
    res.json(response);
});

// 创建API接口  
app.get('/api/getBanners', (req, res) => {
    // 定义响应对象  
    const response = {
        code: 200, // 通常200表示成功  
        data: {
            datas: bannerArray
        },
        msg: "获取成功"
    };

    // 将响应对象转换为JSON并发送给客户端  
    res.json(response);
});


// 分页函数  
function paginate(items, pageNumber, pageSize) {
    const startIndex = (pageNumber - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    return items.slice(startIndex, endIndex);
}

// API端点：获取服装列表（支持分页）  
app.get('/api/getlistdataById', (req, res) => {
    const fs = require('fs');
    const { page = 1, size = 10, tabId = "" } = req.query; // 默认第一页，每页10条数据  
    console.log("tabid="+tabId)
    if(tabId==""){
        res.json({
            code: 500,
            message: 'tabId不能为空',
            data:{} // 返回随机排序后的分页数据  
        });
        return;
    }
    let data;
    switch (tabId) {
        case "005":
            data = JSON.parse(fs.readFileSync('dressdata.json', 'utf8'));
            break;
        case "002":
            data = JSON.parse(fs.readFileSync('computerdatas.json', 'utf8'));
            break;
        case "003":
            data = JSON.parse(fs.readFileSync('phonedatas.json', 'utf8'));
            break;
        case "004":
            data = JSON.parse(fs.readFileSync('foodsdatas.json', 'utf8'));
            break;
        case "001":
            data = JSON.parse(fs.readFileSync('home.json', 'utf8'));
            break;
        case "006":
            data = JSON.parse(fs.readFileSync('freshdatas.json', 'utf8'));
            break;
        case "007":
            data = JSON.parse(fs.readFileSync('furnituredatas.json', 'utf8'));
            break;

        default:
    }
            console.log("🚀 ~ app.get ~ 007:", 007)

    // 读取JSON文件  
    console.log(tabId)
    const paginatedItems = paginate(data.dressDatas, parseInt(page), parseInt(size));
    // 随机排序分页后的数据（可选）  
    const shuffledItems = paginatedItems.sort(() => 0.5 - Math.random());

    res.json({
        code: 200,
        message: 'Success',
        data: shuffledItems // 返回随机排序后的分页数据  
    });
});

// // 启动服务器  
// app.listen(port, () => {
//     console.log(`Server is running on port ${port}`);
// });
const server = app.listen(port, '192.168.1.5', () => {  
    console.log(`服务器已启动，监听端口 ${port}`);  
  });